package com.example.muasic.activity;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.muasic.R;
import com.example.muasic.fragment.QuickControlsFragment;
import com.example.muasic.info.Info;
import com.example.muasic.provider.MusicSqLiteHelper;
import com.example.muasic.service.IService;
import com.example.muasic.service.MusicService;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Favourite extends AppCompatActivity {
    private ImageView Search;
    private ImageView Sort;
    private IService iService;
    private ListView listView;
    private ImageView back;
    private ServiceConnection conn;
    private MusicSqLiteHelper musicSqLiteHelper;
    private static List<String> titleList = new ArrayList<>();
    private static List<Info> musicList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favourite);

        musicSqLiteHelper = new MusicSqLiteHelper(this,"music.db",1);
        Bundle buddle = this.getIntent().getExtras();
        musicList = musicSqLiteHelper.Favoritemusic(musicSqLiteHelper);
        titleList = musicSqLiteHelper.FavoritemusicName(musicSqLiteHelper);

        listView = (ListView)findViewById(R.id.music_fav);
        back = findViewById(R.id.iv_fav_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(PlayMusicActivity.this,
//                        MyFragment.class);
//                startActivity(intent);
                Favourite.this.finish();
            }
        });

        // 开启服务
        Intent intent = new Intent(this, MusicService.class);
        startService(intent);
        conn = new ServiceConnection(){
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                //6. 获取IBinder对象, 以此调用暴露的函数
                iService = (IService) service;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
        };

        //绑定服务

        bindService(intent, conn, BIND_AUTO_CREATE);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,titleList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if(iService.callgetNowMusic()<0||iService.callgetNowMusic()>=musicList.size()) {
                    PlayingActivity.playing(musicList.get(position).getData(), iService);
                    iService.callgetNowmusicList(musicList);
                }else {
                    long nowMusicId=iService.callgetNowMusicId();
                    if (nowMusicId != musicList.get(position).getSongId()) {
                        /*if(iService.callgetNowMusic()!=position){*/
                        PlayingActivity.playing(musicList.get(position).getData(), iService);
                        iService.callgetNowmusicList(musicList);
                    }
                }
                iService.callNowMusic(position);
                QuickControlsFragment.setPosition(position);
                QuickControlsFragment.setmusicList(musicList);
                QuickControlsFragment.updateNowplayingCard();
                Intent intent = new Intent(Favourite.this,
                        PlayingActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("music_send",position);
                bundle.putSerializable("musiclist", (Serializable) musicList);
                intent.putExtras(bundle);
                startActivity(intent);

            }
        });

        Search = findViewById(R.id.iv_fav_search);
        Search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Favourite.this,SearchActivity.class);
                startActivity(intent);
            }
        });
        Sort=findViewById(R.id.iv_sort);

        Sort.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {

                new AlertDialog.Builder(Favourite.this).setTitle("选择排序方式").
                        setItems(R.array.str_body, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, final int which) {
                                String[] aryshop=getResources().getStringArray(R.array.str_body);
                                new AlertDialog.Builder(Favourite.this)
                                        .setMessage(aryshop[which])
                                        .setNegativeButton("ok", new DialogInterface.OnClickListener() {

                                            @Override
                                            public void onClick(DialogInterface arg0, int arg1) {
                                                switch (which){
                                                    case 0://时间
                                                        musicList=musicSqLiteHelper.TimeLocalmusic1(musicSqLiteHelper);
                                                        titleList=musicSqLiteHelper.TimeLocalmusicName1(musicSqLiteHelper);
                                                        update();
                                                        break;
                                                    case 1://字母
                                                        musicList=musicSqLiteHelper.Alphabetmusic1(musicSqLiteHelper);
                                                        titleList=musicSqLiteHelper.AlphabetmusicName1(musicSqLiteHelper);
                                                        update();
                                                        break;
                                                    case 2://作者
                                                        musicList=musicSqLiteHelper.ArtistLocalmusic1(musicSqLiteHelper);
                                                        titleList=musicSqLiteHelper.ArtistLocalmusicName1(musicSqLiteHelper);
                                                        update();
                                                        break;
                                                }
                                            }
                                        }).show();
                            }
                        }).show();
            }});

    }
    private void update(){
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,titleList);
            listView.setAdapter(adapter);
        }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 在activity结束的时候回收资源
        /* iService.callDestroyMusic();*/
        unbindService(conn);
    }
}
